import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {
	static int port = 50000;
	static ServerSocket serverSocket;
	static Socket clientSocket;

	/**
	 * Test with http://localhost:50000/test.html
	 * @param args
	 */
	public static void main(String[] args) {
		int counter = 0;
		
		// we instantiate the server socket
		try {
			serverSocket = new ServerSocket(port);
		} catch (IOException e) {
			System.out.println("Could not listen on port: " + 50);
		    System.exit(-1);
		}

		while(true){
			// we accept connections,bind it to a socket and pass it to a handler
			System.out.println("Server initialized: waiting for connection");
			try {
				clientSocket = serverSocket.accept();
				counter++;
				RequestHandler handler = new RequestHandler(clientSocket);
				handler.start();
				
				//clientSocket.bind(new InetSocketAddress( clientSocket.getRemoteSocketAddress(), port + counter));
			} catch (IOException e) {
				System.out.println("Accept failed");
			    System.exit(-1);
			}
			
			
		}
	}

}
